//引入模块
import Api from './api.js'
import Md5 from './md5.js'
import Cookie from './cookie.js'


new Vue({
    el:"#app",
    data(){
        return{
            flag1:0,

            nc:'',
            tx:'',
            zy:'',
            info:'',
            city:'',
            id:'',
            name:'',
            zh:'',

            //电话
            phone:'',
            phoneinfo:'',
            //邮箱
            email:'',
            einfo:'',
            //身份认证
            verName:'',
            verId:'',

            imageUrl1:'',
            verImgUrl1:'',
            imageUrl2:'',
            verImgUrl2:'',



            fullscreenLoading: false,

            //关注的账号
            carezh:[],
            //账号的粉丝
            fanszh:[]
        }
    },
    mounted() {
        this.sinfo()
    },
    methods:{
        async sinfo(){

            //获取所有用户
            let allinfo = await Api.login()
            //
            let zh = sessionStorage.getItem("zh")
            this.zh = zh
            //判断是否登录
            if(zh==null){
                this.$alert('请先登录！', '温馨提示', {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.$message({
                            type: 'info',
                            message: `action: ${ action }`
                        });
                        window.location.href="./login.html"
                    }
                });
            }
            //获取数据
            let info = await Api.login()
            for(let i=0;i<info.length;i++){
                if(zh==info[i].zh){
                    this.id = info[i].id
                    this.pwd=info[i].pwd
                    this.nc = info[i].nc
                    this.tx = info[i].tx
                    this.zy = info[i].zy
                    this.info = info[i].info
                    this.city = info[i].city
                    this.email = info[i].email
                    this.phone = info[i].phone
                    this.name = info[i].name
                    break;
                }
            }

            //调用关注表的内容
            let careinfo = await Api.careInfo()
            let caremsg = careinfo.data
            //获取
            for (let i=0;i<caremsg.length;i++){
                for (let j=0;j<allinfo.length;j++){
                    //关注的账号
                    if(caremsg[i].user_zh==zh){
                        if(caremsg[i].care_zh==allinfo[j].zh){
                            this.carezh.push(allinfo[j])
                        }
                    }
                    //粉丝的账号
                    if(caremsg[i].care_zh==zh){
                        if(caremsg[i].user_zh==allinfo[j].zh){
                            this.fanszh.push(allinfo[j])
                        }
                    }
                }
            }


        },
        toMessage(info){
            sessionStorage.setItem("ToMessage",JSON.stringify(info))
            window.location.href="My_Messages.html"
        },
        //邮箱输入验证
        e1(){
            let regExp=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            if(regExp.test(this.email)){
                this.flag1=1
            }else {
                this.$message({
                    showClose: true,
                    message: '您的邮箱为空或输入不规范！',
                    type: 'error'
                });
            }
        },
        //邮箱发送验证码
        async goEmail(){
            if(this.flag1==1){
                let zh = sessionStorage.getItem("zh")
                const loading = this.$loading({
                    lock: true,
                    text: '正在发送验证码...',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                let yanzheng = await Api.email(zh,this.email)
                yanzheng = JSON.parse(yanzheng)
                sessionStorage.setItem("email",this.email)
                Cookie.setCookie("yz",yanzheng,'s300')
                setTimeout(() => {
                    loading.close();
                    window.location.href='./Operating.html';
                }, 1000);
            }else {
                this.$message({
                    showClose: true,
                    message: '您的邮箱为空或输入不规范！',
                    type: 'error'
                });
            }
        },
        //重新发送邮箱验证码
        async res(){
            let zh = sessionStorage.getItem("zh")

            const loading = this.$loading({
                lock: true,
                text: 'Loading',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            let yanzheng = await Api.email(zh,this.email)
            if(yanzheng!=null){
                this.$message({
                    showClose: true,
                    message: '发送成功！',
                    type: 'success'
                });
            }
            yanzheng = JSON.parse(yanzheng)
            Cookie.setCookie("yz",yanzheng,'s300')
            setTimeout(() => {
                loading.close();
                window.location.href='./Operating.html';
            }, 1000);
        },
        //邮箱验证
        async success(){
            let zh = sessionStorage.getItem("zh")
            let email = sessionStorage.getItem("email")
            let msg = Cookie.getCookie("yz")

            let yz = Md5.md5(this.einfo)

            if(msg!=null){
                msg = JSON.parse(msg)
            }

            if (msg.data == yz){
                let falg = await Api.updateEmail(zh,email)
                if(falg=='0'){
                    this.$alert('修改成功！返回绑定界面', '温馨提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.$message({
                                type: 'info',
                                message: `action: ${ action }`
                            });
                            window.location.href='./binding.html';
                        }
                    });
                }else {
                    this.$message({
                        showClose: true,
                        message: '服务器忙，请稍后重试！',
                        type: 'error'
                    });
                }
            }else {
                this.$message({
                    showClose: true,
                    message: '验证码错误或已失效！',
                    type: 'error'
                });
            }
        },

        //手机验证码
        async sendPh(){
            let regExp = /^(1[3-8][0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
            if(!regExp.test(this.phone)){
                this.$message({
                    showClose: true,
                    message: '请输入正确的手机号！',
                    type: 'error'
                });
            }else {
                const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                setTimeout(() => {
                    loading.close();
                }, 1000);
                let msg = await Api.sendPhone(this.phone)
                msg = JSON.parse(msg)
                if(msg.code==0){
                    this.$message({
                        showClose: true,
                        message: '发送成功！',
                        type: 'success'
                    });
                }
            }
        },
        //手机验证码
        async restsendPh(){
            let regExp = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
            if(!regExp.test(this.phone)){
                this.$message({
                    showClose: true,
                    message: '请输入正确的手机号！',
                    type: 'error'
                });
            }else {
                const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                setTimeout(() => {
                    loading.close();
                }, 1000);
                let msg = await Api.sendPhone(this.phone)
                msg = JSON.parse(msg)
                if(msg.code==0){
                    this.$message({
                        showClose: true,
                        message: '发送成功！',
                        type: 'success'
                    });
                }
            }
        },
        //验证手机号码
        async verPhone(){
            if(this.phoneinfo!=''){
                let msg = await Api.verifyPhone(this.phone,this.phoneinfo)
                msg=JSON.parse(msg)
                if (msg.code==0){
                    await Api.upPhone(this.zh,this.phone)
                    window.location.href='./phone-bind-done.html'
                }else{
                    this.$message({
                        showClose: true,
                        message: '验证码错误！',
                        type: 'error'
                    });
                }
            }else {
                this.$message({
                    showClose: true,
                    message: '验证码错误或为空！',
                    type: 'error'
                });
            }
        },

        //身份认证
        handleAvatarSuccess1 (res, file) {
            this.imageUrl1 = URL.createObjectURL(file.raw);
            console.log(res)
            let flag = res.code
            if(flag==0){
                this.imgurl1=res.data
            }else if(flag==2){
                this.imgurl1=res.message
            }
        },
        errorAvatar1(err,file){
            console.log("失败")
        },
        beforeAvatarUpload1(file) {
            const isJPG = file.type === 'image/jpeg';
            const isGIF = file.type === 'image/gif';
            const isPNG = file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 1;
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }

            if (!isJPG && !isGIF && !isPNG) {
                this.$message.error('上传头像图片只能是 JPG、GIF、PNG 格式!');
            }else {
                if (!isJPG) {
                }else {
                    return isJPG && isLt2M;
                }
                if (!isGIF) {
                }else {
                    return isGIF && isLt2M;
                }
                if (!isPNG) {
                }else {
                    return isPNG && isLt2M;
                }
            }
        },

        handleAvatarSuccess2(res, file) {
            this.imageUrl2 = URL.createObjectURL(file.raw);
            console.log(res)
            let flag = res.code
            if(flag==0){
                this.imgurl2=res.data
            }else if(flag==2){
                this.imgurl2=res.message
            }
        },
        errorAvatar2(err,file){
            console.log("失败")
        },
        beforeAvatarUpload2(file) {
            const isJPG = file.type === 'image/jpeg';
            const isGIF = file.type === 'image/gif';
            const isPNG = file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 1;
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }

            if (!isJPG && !isGIF && !isPNG) {
                this.$message.error('上传头像图片只能是 JPG、GIF、PNG 格式!');
            }else {
                if (!isJPG) {
                }else {
                    return isJPG && isLt2M;
                }
                if (!isGIF) {
                }else {
                    return isGIF && isLt2M;
                }
                if (!isPNG) {
                }else {
                    return isPNG && isLt2M;
                }
            }
        },
        //身份验证
        async verID(){
            if (this.verName!=''&&this.verId!=''&&this.imgurl1!=null&&this.imgurl2!=null){
                let regExp = /^\d{15}|\d{18}$/;
                if (!regExp.test(this.verId)) {
                    this.$message({
                        showClose: true,
                        message: '请输入正确的身份证号码！',
                        type: 'error'
                    });
                }else {
                    let msg = await Api.verIdandName(this.verName,this.verId)
                    msg = JSON.parse(msg)
                    if(msg.code==0){
                        const loading = this.$loading({
                            lock: true,
                            text: 'Loading',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.7)'
                        });
                        setTimeout(() => {
                            loading.close();
                        }, 1000);
                        let zh = sessionStorage.getItem("zh")
                        let fl = await Api.saveIdandName(zh,this.verId,this.verName)
                        if(fl.code==0){
                            window.location.href='./Verified-bind-done.html'
                        }
                    }

                }
            }else {
                this.$message({
                    showClose: true,
                    message: '请输入信息或上传照片！',
                    type: 'error'
                });
            }
        },


        //立即返回
        nowrest(){
            window.location.href='./binding.html'
        }

    }
})